package com.fanling.venus.project.service;

import com.fanling.venus.common.constant.ProjectContants;
import com.fanling.venus.common.utils.StringUtils;
import com.fanling.venus.project.domain.EpointProject;
import com.fanling.venus.project.mapper.EpointProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EpointProjectService {
    @Autowired
    private EpointProjectMapper epointProjectMapper;

    /**
     * 展示列表
     *
     * @param epointProject
     * @return
     */
    public List<EpointProject> selectProjectList(EpointProject epointProject) {
        return epointProjectMapper.selectProjectList(epointProject);
    }

    /**
     * 新增项目
     *
     * @param epointProject
     * @return
     */
    public int insertMenu(EpointProject epointProject) {
        return epointProjectMapper.insertProject(epointProject);
    }


    /**
     * 校验项目名称是否唯一
     *
     * @param epointProject 项目信息
     * @return 结果
     */
    public String checkProjectNameUnique(EpointProject epointProject) {
        Long projectId = StringUtils.isNull(epointProject.getProjectId()) ? -1L : epointProject.getProjectId();
        EpointProject info = epointProjectMapper.checkProjectNameUnique(epointProject.getProjectName());
        if (StringUtils.isNotNull(info) && info.getProjectId().longValue() != projectId) {
            return ProjectContants.PROJECT_NAME_NOT_UNIQUE;
        }
        return ProjectContants.PROJECT_NAME_UNIQUE;
    }

    /**
     * 删除项目
     *
     * @param projectId
     * @return
     */
    public int deleteProjectById(Long projectId) {
        return epointProjectMapper.deleteProjectById(projectId);
    }

    /**
     * 更新项目
     *
     * @param epointProject
     * @return
     */
    public int updateProject(EpointProject epointProject) {
        return epointProjectMapper.updateProject(epointProject);
    }

    /**
     * 根据项目ID查询信息
     *
     * @param projectId 项目ID
     * @return 项目信息
     */
    public EpointProject selectProjectById(Long projectId) {
        return epointProjectMapper.selectProjectById(projectId);
    }

    /**
     * 加载所有菜单列表树
     *
     * @return
     */
    public List<Map<String, Object>> epointProjectTreeData() {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        List<EpointProject> epointProjectList = epointProjectMapper.selectProjectListAll();
        trees = getTrees(epointProjectList);
        return trees;
    }

    /**
     * 对象转项目树
     *
     * @param epointProjectList
     * @return
     */
    public List<Map<String, Object>> getTrees(List<EpointProject> epointProjectList) {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        EpointProject project = new EpointProject();
        project.setProjectId(0L);
        project.setProjectName("所有项目");
        epointProjectList.add(project);
        for (EpointProject epointProject : epointProjectList) {
            Map<String, Object> deptMap = new HashMap<String, Object>();
            deptMap.put("id", epointProject.getProjectId());
            if (epointProject.getProjectId() == 0) {
                deptMap.put("pId", "");
            } else {
                deptMap.put("pId", "0");
            }
            deptMap.put("name", epointProject.getProjectName());
            deptMap.put("title", epointProject.getProjectName());
            trees.add(deptMap);
        }
        return trees;
    }
}
